<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/17/017
 * Time: 14:46
 */

namespace app\portal\controller;


use app\portal\model\VideoPostModel;
use cmf\controller\AdminBaseController;

class VideoController extends AdminBaseController
{
    protected $videoModel;
    public function _initialize()
    {
        parent::_initialize();
        $this->videoModel = new VideoPostModel();
    }
    /**
     * 视频列表
     */
    public function index()
    {
        $param = $this->request->param();
        $where = [
            'create_time' => ['>=', 0],
            'post_status' => ['>',0]
        ];
        $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
        $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['create_time'] = ['<= time', $endTime];
            }
        }
        $keyword = empty($param['keyword']) ? '' : $param['keyword'];
        if (!empty($keyword)) {
            $where['post_title'] = ['like', "%$keyword%"];
        }
        $videos =  $this->videoModel->where($where)->order('create_time','DESC')->paginate(10);
        $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
        $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
        $this->assign('video',$videos);
        $this->assign('page', $videos->render());
        return $this->fetch();
    }

    /**
     * 添加视频
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 添加视频提交
     */
    public function addPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $data['post']['post_status'] = 1;
            $post = $data['post'];
            $result = $this->validate($post, 'Video');
            if ($result !== true) {
                $this->error($result);
            }
            $this->videoModel->addVideo($data['post']);
            $data['post']['id'] =  $this->videoModel->id;
            $this->success('添加成功!', url('Video/edit', ['id' =>  $this->videoModel->id]));
        }

    }

    /**
     * 编辑视频
     */
    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        $videoPostModel = new VideoPostModel();
        $post = $videoPostModel->where('id','=', $id)->find();
        $this->assign('post', $post);
        return $this->fetch();
    }

    /**
     * 编辑视频的提交
    */
    public function editPost()
    {
        if ( $this->request->isPost() ) {
            $data = $this->request->param();
            $data['post']['post_status'] = 1;
            $post = $data['post'];
            $result = $this->validate($post, 'Video');
            if ($result !== true) {
                $this->error($result);
            }
            $this->videoModel->editVideo($post);
            $this->success('保存成功!');

        }
    }

    /**
     * 视频删除
     */
    public function delete()
    {
        $param           = $this->request->param();
        if (isset($param['id'])) {
            $id           = $this->request->param('id', 0, 'intval');
            $result       = $this->videoModel->where(['id' => $id])->find();
            $data         = [
                'object_id'   => $result['id'],
                'create_time' => time(),
                'table_name'  => 'video_post',
                'name'        => $result['post_title'],
                'user_id'     => cmf_get_current_admin_id()
            ];
            $resultPortal = $this->videoModel
                ->where(['id' => $id])
                ->update(['delete_time' => time(),'post_status'=>0]);
            $this->success("删除成功！", '');

        }

        if (isset($param['ids'])) {
            $ids     = $this->request->param('ids/a');
            $this->videoModel->where(['id' => ['in', $ids]])->update(['delete_time' => time(),'post_status'=>0]);
            $this->success("删除成功！", '');
            }

    }

    /**
     * 文章推荐热门
     * @adminMenu(
     *     'name'   => '文章推荐',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文章推荐',
     *     'param'  => ''
     * )
     */
    public function recommend()
    {
        $param           = $this->request->param();
        $videoModel = $this->videoModel;

        if (isset($param['ids']) && isset($param["yes"])) {
            $ids = $this->request->param('ids/a');

            $videoModel->where(['id' => ['in', $ids]])->update(['recommended' => 1]);

            $this->success("推荐成功！", '');

        }
        if (isset($param['ids']) && isset($param["no"])) {
            $ids = $this->request->param('ids/a');

            $videoModel->where(['id' => ['in', $ids]])->update(['recommended' => 0]);

            $this->success("取消推荐成功！", '');

        }
    }
}